﻿using Common.Helpers;
using DataAccessLayer.Helper;
using MasterEntities.Abstraction;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer.DatabaseOperations
{
    public class Create : AbstractExecutionTemplate
    {
        protected override bool Execute(MasterEntities.Abstraction.IDomainObject obj)
        {
            return GenericDataAccessLayer<IDomainObject>.Instance.Create(GenerateEntityKey(obj), this);
        }

        private MasterEntities.Abstraction.IDomainObject GenerateEntityKey(MasterEntities.Abstraction.IDomainObject obj)
        {
            long ek = 0;
            if (!string.IsNullOrEmpty(obj.BaseClassName()))
                ek = EntityKeyGenerator.Instance.GenerateEntityKey(obj.SchemaName(), (GlobalVariables.Instance.dbConnection != Common.Helpers.Enums.DatabaseConnection.MasterApp2 ? obj.BaseClassName() : obj.GetTableName()));
            else
                ek = EntityKeyGenerator.Instance.GenerateEntityKey(obj.SchemaName(), obj.GetTableName());

            PropertyInfo pi = obj.GetType().GetProperty("Id");
            pi.SetValue(obj, ek, null);
            return obj;
        }


        
    }
}
